﻿@page "/DynamicFormTest1"
@using System.ComponentModel.DataAnnotations
@inject HttpClient Http
@using System.Text.Json
@using System.Reflection;
@using System.ComponentModel.DataAnnotations;
@using System.ComponentModel

<h3>Generated Form</h3>

<link href="_content/AntDesign/css/ant-design-blazor.css" rel="stylesheet">
<link antblazor-css />


<AntDesign.Collapse DefaultActiveKey="@(new[]{"1"})" Accordion>

        @foreach (var key in fields.Keys)
        {
            <AntDesign.Panel Header="This is panel header 1" Key="1">
                <AntDesign.Form Model="@model"
                                OnFinish="OnFinish"
                                OnFinishFailed="OnFinishFailed"
                                LabelColSpan="8"
                                WrapperColSpan="16"
                                ValidateOnChange>

                    @foreach (var field in fields[key])
                    {
                        <AntDesign.FormItem Name="@field.Key">
                            @InputRender(field.Value)
                        </AntDesign.FormItem>
                    }

                    <AntDesign.FormItem WrapperColOffset="8" WrapperColSpan="16">
                        <AntDesign.Button Type="@AntDesign.ButtonType.Primary" HtmlType="submit">
                            Submit
                        </AntDesign.Button>
                    </AntDesign.FormItem>
                </AntDesign.Form>
            </AntDesign.Panel>
        }
    </AntDesign.Collapse>



<!-- AntDesign -->
<script src="_content/AntDesign/js/ant-design-blazor.js"></script>
<script antblazor-js></script>

@code {

    private RenderFragment InputRender(PropertyInfo property)
    {
        return property switch
        {
            _ when property.PropertyType.GetCustomAttribute<DataTypeAttribute>()?.DataType == DataType.Password =>@<AntDesign.InputPassword></AntDesign.InputPassword>,
            _ when property.PropertyType == typeof(bool) =>@<AntDesign.Checkbox></AntDesign.Checkbox>,
            _ when property.PropertyType == typeof(DateOnly?) =>@<AntDesign.DatePicker TValue="DateOnly?"></AntDesign.DatePicker>,
            _ =>@<AntDesign.Input TValue="string"></AntDesign.Input>,
        };
    }

    private RenderFragment MeasurementHeader => __builder =>
    {
        <div class="mt-1">Measurement</div>
    };



    protected override async Task OnInitializedAsync()
    {

    }


    private Dictionary<string, Dictionary<string, PropertyInfo>> fields = new();
    private Dictionary<string, object> models = new();

    //private Dictionary<string, PropertyInfo> fields = new();
    private Model model = new();
    protected override void OnInitialized()
    {
        fields.Add("Model", typeof(Model).GetProperties().ToDictionary(x => x.Name, x => x));
        fields.Add("StudentModel", typeof(Model).GetProperties().ToDictionary(x => x.Name, x => x));
        models.Add("Model", new Model());
        models.Add("StudentModel", new Model());
        base.OnInitialized();
    }

    public class Model
    {
        [Required, DisplayName("User Name")]
        public string Username { get; set; }

        [DisplayName("Birthday")]
        public DateOnly? Birthday { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [DisplayName("Password")]
        public string Password { get; set; }

        [DisplayName("RememberMe")]
        public bool RememberMe { get; set; } = true;
    }

    private void OnFinish(EditContext editContext)
    {

    }

    private void OnFinishFailed(EditContext editContext)
    {

    }
}